import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter, findSubCategoryGoods } from '@/api/category'
const useSubCate = () => {
  const subCate = ref({})
  const route = useRoute()
  findSubCategoryFilter(route.params.id).then(data => {
    subCate.value = data.result
    // 品牌  cateList.value.brands  没有‘全部’分类  我们自己添加一个全部的分类
    // 在品牌list中的头上添加一个全部分类  unshift

    // 给每一个品牌添加一个专门用来控制是否激活的字段 selected true 激活 false 不激活
    subCate.value.brands.forEach(it => { it.selected = false })

    subCate.value.brands.unshift({
      name: '全部',
      id: null,
      selected: true
    })

    subCate.value.saleProperties.forEach((item) => {
    // 给每一个规格添加一个专门用来控制是否激活的字段 selected true 激活 false 不激活
      item.properties.forEach(it => { it.selected = false })

      item.properties.unshift({
        name: '全部',
        id: null,
        selected: true
      })
    })
    subCate.value = data.result
  })
  return { subCate }
}

const useLoadDate = () => {
  const route = useRoute()
  let reqParams = {
    page: 1,
    pageSize: 20,
    categoryId: route.params.id,
    sortField: null, // 排序类别
    sortMethod: 'desc', // 排序的方式
    attrs: [], // 商品属性
    brandId: null, // 品牌名称
    inventory: null, // 是否只显示有库存
    onlyDiscount: null // 是否只显示有优惠
  }
  const goodList = ref([]) // 保存查询结果
  const isLoading = ref(false)
  const isFinished = ref(false)
  const loadDate = () => {
    isLoading.value = true

    findSubCategoryGoods(reqParams).then(data => {
      console.log('findSubCategoryGoods', data)
      // 新数据要追加到数组中
      goodList.value.push(...data.result.items)
      // 页码加1
      reqParams.page++
      // 判断是否加载完成
      if (data.result.items.length === 0) {
        isFinished.value = true
      }
      isLoading.value = false
    })
  }
  const hFilterChange = (filterParam) => {
    console.log('父组件收到了筛选条件', filterParam)
    // 更新条件
    // reqParams.brandId = filterParam.brandId
    // reqParams.attrs = filterParam.attrs
    reqParams = { ...reqParams, ...filterParam }

    // 重置页码,清空数据,isFinished为false
    reqParams.page = 1
    goodList.value = []
    isFinished.value = false
    // 重发请求
    loadDate()
  }
  // const hFilterChange = (filterParam) => {
  //   console.log('父组件收到了筛选条件', filterParam)
  //   // 更新条件
  //   // reqParams.inventory = sortParam.inventory
  //   // reqParams.onlyDiscount = sortParam.onlyDiscount
  //   reqParams = { ...reqParams, ...filterParam }
  //   loadDate()
  // }
  // const hSortChange = (sortParam) => {
  //   console.log('父组件收到了排序条件', sortParam)
  //   reqParams = { ...reqParams, ...sortParam }
  //   // 重新发送
  //   loadDate()
  //   return { isFinished, isLoading, goodList, hFilterChange, hSortChange, loadDate, hSortChange }
  // }
  const hSortChange = (sortParam) => {
    console.log('父组件收到了排序条件', sortParam)
    // 更新条件
    // reqParams.inventory = sortParam.inventory
    // reqParams.onlyDiscount = sortParam.onlyDiscount
    reqParams = { ...reqParams, ...sortParam }

    // 重置页码,清空数据,isFinished为false
    reqParams.page = 1
    goodList.value = []
    isFinished.value = false
    // 重发请求
    loadDate()
  }

  loadDate()

  return { isFinished, isLoading, goodList, hFilterChange, hSortChange, loadDate }
}
export { useSubCate, useLoadDate }
